package com.allwees.bs.module.oms.dao;

import com.allwees.bs.module.oms.entity.Order;
import com.allwees.core.dao.mybatis.dao.BaseDao;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * @author yangylsky
 * @since 2019-08-21 09:26
 */
public interface OrderDao extends BaseDao<Order> {
    @Select("SELECT * FROM o_order where no = #{no}")
    Order findByNo(String no);

    /**
     * 查询未结束流程的订单编号
     *
     * @return
     */
    @Select({"<script>",
            "SELECT",
            "  o.uuid",
            " FROM o_order o",
            " WHERE shipping_at>from_unixtime(unix_timestamp() - 3600*24*30) ", //仅查询30天内发货的订单
            " AND express_code is not null",
            " AND state in('SHIPPED', 'WAIT_DELIVER', 'DELIVERED')",
            "</script>"
    })
    List<String> getUnfinishedOrderUuids();

    @Select({"<script>",
            "SELECT * ",
            " FROM o_order o where o.state = 'CREATED' AND o.payment_expired_at &lt;= #{time}",
            "</script>"
    })
    List<Order> findUnPaidOrderList(Date time);

    @Select({"<script>",
            "SELECT * FROM o_order o ",
            "where o.state = 'CREATED' AND o.payment_expired_at > #{paymentExpiredFrom} AND o.payment_expired_at &lt;= #{paymentExpiredTo}",
            "</script>"
    })
    List<Order> findUnPaidUnExpireOrderList(Date paymentExpiredFrom, Date paymentExpiredTo);

    @Select({"<script>",
            "SELECT * FROM o_order o ",
            "where o.state = 'WAIT_SHIP' AND o.shipping_wait_at &lt; #{shippedExpiredAt}",
            "</script>"
    })
    List<Order> findOutOvertimeShippingOrders(Date shippedExpiredAt);

	@Select({"<script>",
			"SELECT o.uuid FROM o_order o ",
			"where o.state = 'DELIVERED' AND o.deliver_at &lt; #{expireDate}",
			"</script>"
	})
    List<String> findExpireReceivedUuids(Date expireDate);

    @Select({"<script>",
            "SELECT o.uuid FROM o_order o ",
            "where o.state = 'WAIT_SHIP' AND synced = false ",
            "</script>"
    })
    List<String> findCanSyncOrders();

    @Select({"<script>",
            "SELECT * FROM o_order o ",
            "where created_at > '2020-08-31 23:59:59'  and state != 'CANCELED' and state != 'CREATED' ",
            "</script>"
    })
    List<Order> findListByIdRange();
}
